/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package delete;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.primefaces.json.JSONException;
import org.primefaces.json.JSONObject;

/**
 *
 * @author Vick
 */
public class Forecast {

    public Boolean checkingWeather(String city) {

        try {
            URL url = new URL("http://api.openweathermap.org/data/2.5/forecast/daily?q=" + city + "&mode=json&units=metric&cnt=2");

            //Get the JSON result from the URL
            Scanner scan = new Scanner(url.openStream());
            String jsonResult = new String();

            while (scan.hasNext()) {
                jsonResult += scan.nextLine();
            }

            scan.close();

            //Build a JSON object
            JSONObject obj = new JSONObject(jsonResult);

            if (obj.getJSONArray("list").getJSONObject(1).getJSONArray("weather").getJSONObject(0).getString("main").equalsIgnoreCase("Rain")) {
                return true;
            }

//	    // get the first result
//	    JSONObject res = obj.getJSONArray("results").getJSONObject(0);
//	    System.out.println(res.getString("formatted_address"));
//	    JSONObject loc
//		    = res.getJSONObject("geometry").getJSONObject("location");
//	    System.out.println("lat: " + loc.getDouble("lat")
//		    + ", lng: " + loc.getDouble("lng"));
            return false;
        } catch (MalformedURLException ex) {
            System.out.println("The city is incorrect.");
            return false;
        } catch (IOException | JSONException ex) {
            Logger.getLogger(Forecast.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    
    }

    
}
